package com.moto.mapper;

import com.moto.Vo.BolgFindAllVO;
import com.moto.entity.Bolg;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.moto.entity.Favorite;
import org.apache.ibatis.annotations.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author zyz
 * @since 2023-08-29
 */
@Mapper
public interface BolgMapper extends BaseMapper<Bolg> {

    /**
     * 热门文章
     * @return
     */
    @Select("select b.bolg_id ,b.bolg_title,b.bolg_watch ,u.nickname ,b.imgs,b.pub_time,b.likess,b.u_id,u.photo  from bolg b left join user u on b.u_id = u.id where b.is_delete=1 order by b.bolg_watch DESC")
    List<BolgFindAllVO> findHotBolg();

    /**
     * 发布文章
     * @param bolg
     * @return
     */
    @Insert("insert into bolg(u_id,bolg_title,bolg_watch,bolg_author,imgs,bolg_article,bolg_sort_id,likess,collect,pub_time,is_delete) values(#{uId},#{bolgTitle},0,#{bolgAuthor},#{imgs},#{bolgArticle},#{bolgSortId},0,0,#{pubTime},1) ")
    void publish(Bolg bolg);

    /**
     * 增加浏览量
     */
    @Update("UPDATE bolg SET bolg_watch = bolg_watch + 1 WHERE bolg_id = #{bolgId}")
    int incrementViews(@Param("bolgId") Integer bolgId);

    @Update("UPDATE bolg SET likess = likess + 1 WHERE bolg_id = #{bolgId}")
    int like(@Param("bolgId") Integer bolgId);


    @Transactional
    @Update("UPDATE bolg SET collect = collect + 1 WHERE bolg_id = #{bolgId}")
    int collect(Integer bolgId);

    @Select("SELECT b.bolg_title,b.bolg_author,b.imgs,b.bolg_watch FROM bolg b LEFT JOIN favorite f ON b.bolg_id=f.bolg_id JOIN user u on  f.u_id=u.id WHERE u.id=#{id}}")
    List<Favorite> favoriteList();


    @Select("select b.bolg_id ,b.bolg_title,b.bolg_watch ,u.nickname ,b.imgs,b.pub_time,b.likess,b.u_id,u.photo  from bolg b left join user u on b.u_id = u.id where b.is_delete=1 and b.bolg_sort_id=#{id} order by b.bolg_watch DESC")
    List<BolgFindAllVO> findByIdBolg(int id);

    @Select("select b.bolg_id ,b.bolg_title,b.bolg_watch ,u.nickname ,b.imgs,b.pub_time,b.likess,b.u_id,u.photo  from bolg b left join user u on b.u_id = u.id where b.is_delete=1 order by b.pub_time DESC")
    List<BolgFindAllVO> findAllBolg();
}
